﻿using Microsoft.AspNetCore.Identity;
using System;
using System.Linq;
using YYTea.MOdel.Model;
using YYTea.ORM;

namespace YYTea.DbIntializer
{
    public class YYTeaDbInitiazer
    {


        public YYTeaDbInitiazer()
        {

        }
        public static void Initializer(YYTeaContext context)
        {
            context.Database.EnsureCreated();


            if (context.Role.Any())
            {
                return;
            }
            var roles = new Role[]
            {
                new Role{Id = "1", Num = "1" ,Name = "超级管理员",NormalizedName = "SuperAdmin"},
                new Role{Id = "2", Num = "2" ,Name = "管理员" ,NormalizedName = "Admin"},
                new Role{Id = "3",Num = "3" ,Name = "普通用户" ,NormalizedName = "commonUser"}
            };
            context.Roles.AddRange(roles);
            context.SaveChanges();


            if (context.UserInfos.Any())
            {
                return;
            }
            var users = new UserInfo[]
            {
                new UserInfo { Id = "1", EmailConfirmed = true, UserName = "Admin" , PasswordHash = "Admin123." , Email = "Admin@qq.com" , Address = "宿舍" ,
                    AddTime = Convert.ToDateTime("2020/5/5"), PhoneNumber = "18888888888"  ,  Qq = "123456789",Sex = Sex.男,NormalizedUserName = "Admin",
                     NormalizedEmail = "123456@qq.com",TwoFactorEnabled = false,PhoneNumberConfirmed = false
                   /* RoleName = context.Role.SingleOrDefault(s => s.Num==1).Name*/ 
                   
                },
                new UserInfo {  Id = "2",EmailConfirmed = true, UserName = "01" , PasswordHash = "Admin01." , Email = "147258@qq.com" , Address = "宿舍01" ,
                    AddTime = Convert.ToDateTime("2020/5/5"), PhoneNumber = "17777777777"  , Qq = "147258369",Sex = Sex.女,NormalizedUserName = "01",
                    NormalizedEmail = "147258@qq.com",TwoFactorEnabled = false,PhoneNumberConfirmed = false
                    /*RoleName = context.Role.SingleOrDefault(s => s.Num==3).Name*/}
            };
            context.UserInfos.AddRange(users);
            context.SaveChanges();

            if (context.UserRole.Any())
            {
                return;
            }
            var userRoles = new UserRole[]
            {
                new UserRole { Num = 1, RoleId = context.Role.SingleOrDefault(s=>s.Id == "1").Id,
                    UserId = context.UserInfos.SingleOrDefault(s=>s.Id == "1").Id },
                new UserRole { Num = 2, RoleId = context.Role.SingleOrDefault(s=>s.Id == "3").Id,
                    UserId = context.UserInfos.SingleOrDefault(s=>s.Id  == "2").Id }
            };
            context.UserRoles.AddRange(userRoles);
            context.SaveChanges();



            if (context.Products.Any())
            {
                return;
            }
            var products = new Product[]
            {

                new Product { NumberSe = 1, Name = "烤奶", Price= 25, StorkNum  = 10, Detail = "唇齿留香", Img = "/youyouMiljyTea/Img/烤奶.jpg"/*, ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s.SizeNum == 1).ShoppingCarID*/},
                new Product { NumberSe = 2 ,Name = "芒果汁", Price= 15, StorkNum  = 10, Detail = "果香浓厚", Img = "/youyouMiljyTea/Img/烤奶.jpg"/*,ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s.SizeNum == 1).ShoppingCarID*/}
            };
            context.Products.AddRange(products);
            context.SaveChanges();


            if (context.ShoppingCars.Any())
            {
                return;
            }
            var shoppingCar = new ShoppingCar[]
            {

                new ShoppingCar{  SizeNum = 1,UserInfoID = context.UserInfos .SingleOrDefault(s=>s.Id == "1").Id, DateTime = DateTime.Now,
                   /*ProductId = context.Products.FirstOrDefault(s=>s.NumberSe == 1).ProductID*/ 
                   /* Products = context.Products.FirstOrDefault(s=>s.NumberSe == 1)*/},
                new ShoppingCar{  SizeNum = 2,UserInfoID = context.UserInfos .SingleOrDefault(s=>s.Id == "2").Id, DateTime = DateTime.Now,
                   /*ProductId = context.Products.FirstOrDefault(s=>s.NumberSe == 2).ProductID*/
                    /*Products = context.Products.FirstOrDefault(s=>s.NumberSe == 2)*/}

            };
            context.ShoppingCars.AddRange(shoppingCar);
            context.SaveChanges();


            if (context.Orders.Any())
            {
                return;
            }
            var orders = new Order[]
            {
                new Order {  Money = 20 , AddDate = Convert.ToDateTime("2020/10/29"), state = "未支付",
                    ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s .SizeNum == 1).ShoppingCarID},

                new Order {  Money = 20 , AddDate = Convert.ToDateTime("2020/10/29"), state = "未支付",
                    ShoppingCarID = context.ShoppingCars.SingleOrDefault(s=>s .SizeNum == 2).ShoppingCarID}

            };
            context.Orders.AddRange(orders);
            context.SaveChanges();

        }

    }
}
